package com.gaohe.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Arrays;

//AOP
//AOP --> 动态代理
@Slf4j
@RestControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result uniqueException(Throwable throwable) {
        String message = throwable.getCause().getMessage();
        log.info("拦截异常：{}", message);
//        System.err.println(throwable.getCause().getMessage());
        // java 名称已重复
            if (message.contains("sys_tag.name")) {
            String[] split = message.split(" ");
            String keyWord = split[2];
            return Result.error("标签名称：" + keyWord + " 已重复");
        }
        return Result.error("操作异常");
    }

}
